﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserManager
/// </summary>
public class UserManager
{
    #region Instance
    private static UserManager instance;
    public static UserManager Instance
    {
        get
        {
            instance = new UserManager();
            return instance;
        }
    }
    #endregion

    private static readonly String name = "_user";
    UserEntity CurrentUser;
    private UserManager()
    {
        try
        {
            if (HttpContext.Current.Session[name] != null)
            {
                CurrentUser = (UserEntity)HttpContext.Current.Session[name];
            }
            else
            {
                Reset();
            }
        }
        catch
        {
            Reset();
        }
    }

    public UserEntity Getuser()
    {
        return CurrentUser;
    }

    public void Setuser(UserEntity _user)
    {
        this.CurrentUser = _user;
        HttpContext.Current.Session[name] = this.CurrentUser;
    }

    public void Reset()
    {
        CurrentUser = new UserEntity();
        HttpContext.Current.Session[name] = CurrentUser;
    }

    public bool CheckDistributorAdminAuthen()
    {
        try
        {
            BookContextDataContext db = new BookContextDataContext();
            User user = db.Users.Where(u => u.Account == CurrentUser.Account && u.RoleID == 1).FirstOrDefault();
            if (user != null)
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }


    public bool CheckDistributorPublisherAuthen()
    {
        try
        {
            BookContextDataContext db = new BookContextDataContext();
            User user = db.Users.Where(u => u.Account == CurrentUser.Account && u.RoleID == 2).FirstOrDefault();
            if (user != null)
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
}